package com.example.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author lei yu
 * @date 2024/05/15
 */
public class JDBCDemo1 {

    /**
     *
     * oracle.jdbc.OracleDriver
     *     # service name -> jdbc:oracle:thin:@//localhost:1521/orcl
     *     # sid          -> jdbc:oracle:thin:@localhost:1521:orcl -> 多个实例我们则使用 sid
     *     url: jdbc:oracle:thin:@//localhost:1521/XE
     */
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        // 1,注册驱动：⼿动加载字节码⽂件到 JVM 中
        Class.forName("com.mysql.cj.jdbc.Driver");
        Class.forName("oracle.jdbc.OracleDriver");

        // 2,连接数据库：参数1：url、参数2：用户、参数3：密码
        // service name -> jdbc:oracle:thin:@//localhost:1521/orcl
        // sid          -> jdbc:oracle:thin:@localhost:1521:orcl -> 多个实例我们则使用 sid
        // mysql 如下
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo", "admin", "admin@me4");

        // 3,获取执行SQL语句的对象
        Statement stmt = conn.createStatement();


        // 4,定义执行SQL 语句
         String sql = "insert  bonus values ('张三', 'java研发', 10000,100)";
        // String sql = "update bonus set comm =200 where ENAME = '张三'";
//        String sql = "delete from bonus where ENAME = '张三'";
        // 执行完会返回受影响的行数
        int row = stmt.executeUpdate(sql);
        // 5，处理结果
        if (row >= 0) {
            System.out.println("执行成功");
        }
        // 6，释放资源
        stmt.close();
        conn.close();

    }
}
